package ua.org.inspace.nosqlgui;

import ua.org.inspace.exception.NoSqlException;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by: Mykhailo Matiiasevych
 * Date: 03.11.11
 * Time: 9:34
 */
public class ScriptUtils {
    public void saveScriptAs(File tofile, String text) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(tofile, false));
            writer.append(text);
            writer.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception ex) {
                    /**/
                }
            }
        }

    }

    public List<File> getScriptsList(String path) {
        List<File> result = new ArrayList<File>(0);
        File scriptsDir = new File(path);
        if (scriptsDir.exists() && scriptsDir.isDirectory()) {
            Collections.addAll(result, scriptsDir.listFiles());
        }
        return result;
    }

    public void copyScripts(String path, String bundledScripts[]) throws NoSqlException {
        File scriptsDir = new File(path);
        if (!scriptsDir.exists()) {
            scriptsDir.mkdir();
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            for (String script : bundledScripts) {
                try {
                    in = getClass().getClassLoader().getResourceAsStream("ua/org/inspace/scripts/" + script);
                    if (in == null) {
                        Logger.getLogger("stderr").info("No script file "+script);
                        continue;
                    }
                    out = new FileOutputStream(scriptsDir.getAbsolutePath() + "/" + script);
                    byte[] buffer = new byte[1024];
                    int count = 0;
                    while ((count = in.read(buffer)) != -1) {
                        out.write(buffer, 0, count);
                    }
                    out.flush();
                } catch (Exception ex) {
                    throw new NoSqlException(ex);
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception ex) {
            throw new NoSqlException(ex);
        }
    }


    public String loadScript(File file) {
        StringBuilder builder = new StringBuilder();

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append("\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception ex) {
                    /**/
                }
            }
        }
        return builder.toString();
    }
}
